import requests
import json

if __name__ == '__main__':
    # 请求头
    headers = {
        'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36"
    }
    url = 'http://scxk.nmpa.gov.cn:81/xk/itownet/portalAction.do?method=getXkzsList'
    params = {
        'n': 'true',
        'page': 1,
        'pageSize': 15,
        'conditionType': 1,
        'productName': '',
        'applyname': '',
        'applysn': ''
    }
    json_ids = requests.post(url=url, params=params, headers=headers).json()
    id_list = []
    all_data_list = []

    for dic in json_ids['list']:
        id_list.append(dic['ID'])
    print(id_list)

    post_url = 'http://scxk.nmpa.gov.cn:81/xk/itownet/portalAction.do?method=getXkzsById'
    for id in id_list:
        data = {
            'id': id
        }
        dtl_json = requests.post(url=post_url, params=data, headers=headers).json()
        print(dtl_json)
        all_data_list.append(dtl_json)
    with open('药监局.json', 'w', encoding='utf-8') as fp:
        json.dump(all_data_list, fp, ensure_ascii=False)
    print('over!!!')
